package com.foreveross.service;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.SysMenuManagerDAO;
import com.foreveross.dao.SysRoleManagerDAO;
import com.foreveross.dao.SysRoleMenuManagerDAO;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.SysRole;

@Service("sysRoleManagerService")
@Transactional
public class SysRoleManagerService {

	@Resource
	SysRoleManagerDAO sysRoleManagerDAO;
	@Resource
	SysMenuManagerDAO sysMenuManagerDAO;
	@Resource
	SysRoleMenuManagerDAO sysRoleMenuManagerDAO;

	public <T> void saveOrUpdate(T entity, String type,String id) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			sysRoleManagerDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			sysRoleManagerDAO.updateObject(entity,id);
		}

	}

	public Object getObjectById(Class clazz, String id) throws Exception {
		return sysRoleManagerDAO.findById(clazz, id);
	}

	public Object getObjectByIntId(Class clazz, int id) throws Exception {
		return sysRoleManagerDAO.findByIntId(clazz, id);
	}

	public List<SysRole> getSysRoleList(String name, int pageIndex, int pageSize)
			throws Exception {
		return sysRoleManagerDAO.findSysRoleList(name, pageIndex, pageSize);
	}

	public long getSysRoleListCount(String name) throws Exception {
		return sysRoleManagerDAO.findSysRoleListCount(name);
	}

	public void deleteSysRole(String ids) throws Exception {
		for (String id : ids.split(",")) {
			sysRoleMenuManagerDAO.deleteSysRoleMenuByRoleId(id);
			sysRoleManagerDAO.deleteSysRole(id);
		}
	}

	public JSONArray getUserRoleList(String userId) throws Exception {
		return sysRoleManagerDAO.getUserRoleList(userId);
	}

}
